import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
import servers
import utiles

class Email():
    """Gestor de emails para cumpleanos"""

    def __init__(self, username, password, host, port, email_from, subject,
                 html_path, image_path, destinatario,debug=False):
        """Inicializador de la clase"""
        
        self.username = username
        self.password = password
        self.host = host
        self.port = port
        self.email_from = email_from
        self.subject = subject
        self.debug = debug

        self.load_files(html_path, image_path, destinatario)

    def load_files(self, html_path, image_path, destinatario):
        """Carga el archivo HTML y la imagen"""
        html_file = open(html_path, 'r')
        self.html = html_file.read()
        self.html = self.html.replace("MINOMBRE",destinatario)
        html_file.close()

        image_file = open(image_path, 'r')
        self.image = image_file.read()
        image_file.close()

        #print self.image

    def create_msg(self, email_to):
        """Crea el mensaje html con la imagen incrustada en
        texto plano codificado. Retorna el mensaje listo para enviar"""

        msg = MIMEMultipart('related')
        #msg.set_charset('utf-8')
        msg['Subject'] = self.subject
        msg['From'] = self.email_from
        msg['To'] = email_to
        msg_html = MIMEText(self.html, 'html')
        #msg_html.set_charset('utf-8')
        msg.attach(msg_html)
        #msg_image = MIMEImage(self.image)
        #msg_image.add_header('Content-ID', '<image1>')
        #msg.attach(msg_image)

        return msg.as_string()

    def send_email(self, email_from, email_to, msg):
        """Envia el msg por correo SMTP desde from hacia to"""
##        server = smtplib.SMTP()
##        server.set_debuglevel(self.debug)
##        server.connect(self.host, self.port)
##        server.ehlo()
##        server.starttls()
##        server.ehlo()
##        server.login(self.username, self.password)
##        server.sendmail(email_from, email_to, msg)
##        server.quit()
        sucursal = int(utiles.getSucursalActiva())-1
        host = servers.mailservername[sucursal]
        port = servers.mailport[sucursal]
        username = servers.mailuser[sucursal]
        password = servers.mailpassword[sucursal]

        mailServer = smtplib.SMTP(host,port)
        mailServer.ehlo()
        mailServer.starttls()
        mailServer.ehlo()
        mailServer.login(username,password)
        mailServer.sendmail(email_from, email_to, msg)

    def send_many_emails(self, dict_name_email):
        """Envia un email por cada uno de los destinatarios en el diccionario"""
        for name, email in dict_name_email.iteritems():
            print name, email
            msg = self.create_msg(email)
            msg = msg.replace('{ % name % }', name)
            #print msg
            self.send_email(self.email_from, email, msg)
